package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.geom.Matrix3D;
	import flash.geom.Vector3D;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.utils.Dictionary;
	import flash.utils.setInterval;
	
	import popple.cameras.Camera;
	import popple.display.DisplayObject;
	import popple.display.Image;
	import popple.display.Sprite;
	import popple.display.Stage;
	import popple.display.TBitmap;
	import popple.display.text.TextField;
	import popple.events.EngineEvent;
	
	public class MyGame extends Stage
	{
		[Embed(source="background.png")]
		private var img:Class;
		[Embed(source="element.png")]
		private var element:Class;
		
		private var bd:BitmapData;
		
		private var _total:uint=1600;
		private var _map:Dictionary=new Dictionary;
		private var _numPerPerticle:uint=200;
		private var _objs:Array=[];
		private var _camera:Camera;
		private var _can:Matrix3D;
		private var _angle:Number=0;
		private var bmp:TBitmap;
		public function MyGame(bitmap:BitmapData=null)
		{
			addEventListener(EngineEvent.READY,onComplete);
			_can=new Matrix3D
			_can.appendRotation(45,Vector3D.X_AXIS);
		}
		private function onComplete(e:Event):void
		{
			//_camera=new Camera;
			//_camera.active=true;
			bd=(new element as Bitmap).bitmapData;
			bmp=new TBitmap(bd);
			
			
			//buildMore(1);
			
			buildPerticleMap();
			setInterval(buildParticle,300);
			
			//setInterval(changeCamera,10000,_map);
			
			
			/*var tt:TextField=new TextField;
			    
  			    tt.wordWarp=true;
				var textFormat:TextFormat=new TextFormat
				textFormat.color=0x99cc00;
				//textFormat.size=12;
			tt.autoSize=TextFieldAutoSize.LEFT;
			tt.text="哇哈哈哇哈哈哈哇哈哈哇哈哈哇哈哈哈哈哇哈哈哇哈哈哈哈哇哈哈哇哈哈哈哈哇哈哈哇哈哈";
			tt.setTextFormat(textFormat);
			tt.x=200;
			tt.y=200;
			
			
			
				
				
				
			addChild(tt);*/
		}
		
		private function changeCamera(rndSource:*):void
		{
			var cam:Matrix3D=new Matrix3D
				cam.appendRotation(Math.random()*30,Vector3D.Y_AXIS);
				cam.appendRotation(Math.random()*30,Vector3D.X_AXIS);
				//cam.appendRotation(Math.random()*360,Vector3D.Y_AXIS);
			
            if(rndSource is Array)
			{
				var num:int=Math.random()*_objs.length;
			    _camera.watch(_objs[num],cam);
			}
			else if(rndSource is Dictionary)
			{
				for each(var obj:DisplayObject in rndSource)
				{
					if(Math.random()*100>70)
					{
						_camera.watch(obj,cam);
						return;
					}
				}
			}
		}
		
		private function buildMore(type:uint=1):void
		{
			var tf:TextFormat=new TextFormat;
			    tf.color=0x00ff00;
				
			var  k:Number;
			for(var i: uint=0;i<_total;i++)
			{
				if(type==1){
				var obj:Image=new Image(bmp)
				obj.x=Stage.width*Math.cos(i)+Stage.width/2;
				obj.y=Stage.width*Math.sin(i);
				obj.z=Math.random()*10000;
				obj.color=0xff00ff;
				obj.data=Math.random()*10+30;
				obj.scaleX=obj.scaleY=2.5;
				obj.addEventHandle("enterFrame",onMove);
				_objs.push(addChild(obj));
				}else
				{
					var bb:TextField=new TextField();
					bb.autoSize=TextFieldAutoSize.LEFT;
					bb.text="who's your daddy?"
					//bb.text="谁是你老爹?"
						bb.setTextFormat(tf);
					bb.x=Stage.width*Math.cos(i)+Stage.width/2;
					bb.y=Stage.width*Math.sin(i);
					bb.scaleX=bb.scaleY=5
					bb.z=Math.random()*10000;
					bb.alpha=Math.random()*1.0;
					bb.rotationX=Math.random()*360;
					bb.rotationY=Math.random()*360;
					bb.rotationZ=Math.random()*360;
					//bb.color=0x9999ff;
					bb.data=Math.random()*10+30;
					
					bb.addEventHandle("enterFrame",onMove);
					_objs.push(addChild(bb));
				}
			}
			
		}
		private function onMove(obj:popple.display.DisplayObject):void
		{
			obj.z-=obj.data;
			if(obj.z<-100)
			{
				obj.z=8000;
				obj.x=Stage.width*Math.cos(Math.random()*360)+Stage.width/2;
				obj.y=Stage.width*Math.sin(Math.random()*360);
				
				
			}
			
			
		}
		private function buildPerticleMap():void
		{
			//var particles:Array=getParticles();
			for(var i:uint=0;i<_total;i++)
			{
				var obj:Particle=new Particle;
				    obj.bitmap=(new TBitmap(bd));
				_map[obj]=obj;
			}
		}
		private function buildParticle():void
		{
			if(Math.random()*10>8)return;
			var num:int;
			var tx:Number=Math.random()*Stage.width;
			var ty:Number=Math.random()*Stage.height;
			var tz:Number=Math.random()*1000;
			
			var color:int=Math.random()*0xffffff+0x999999;
			for each(var obj:Particle in _map)
			{
				if(obj.isWorking()==false)
				{
					this.addChild(obj);
					obj.move(new Vector3D(tx,ty,tz,0));
					obj.color=color;
					obj.alpha=Math.random()*1.0+.1;
					
					var rnd:int=Math.random()*100;
					
					
					var scale:Number=Math.random()*1.0+.1;
					obj.scaleX=obj.scaleY=scale;
					//obj.rotationX=Math.random()*360;
					//obj.rotationY=Math.random()*360;
					num++;
					if(num==_numPerPerticle)
					{
						
						return;
					}
				}
			}
		}
        private function buildBomb(x:Number,y:Number,num:uint):void
		{
			var obj:Sprite;
			for(var i:uint=0;i<num;i++)
			{
				obj=new Sprite;
				
			}
		}
		private function createSprite(bmp:BitmapData,color:uint,alpha:Number):Sprite
		{
			var sprite:Sprite=new Sprite;
			    sprite.color=Math.random()*0xffffff;
				sprite.alpha=Math.random()*1.0;
			return sprite;
		}
	}
}